<template>
    <div :class="{'ly-is-full':isFull}">
        <div class="tableSelect" ref="tableSelect">
            <el-form :inline="true" :model="formInline" label-position="left">
                <el-form-item label="关键字：">
                    <el-input size="default" v-model.trim="formInline.search" maxlength="60"  clearable placeholder="脚本名称" @change="search" style="width:200px"></el-input>
                </el-form-item>
                <el-form-item label="" v-show="hasPermission('scriptfm','Search')"><el-button  @click="search" type="primary" icon="Search">查询</el-button></el-form-item>
                <el-form-item label=""><el-button  @click="handleEdit('','reset')" icon="Refresh">重置</el-button></el-form-item>
                <el-form-item label="" v-show="hasPermission('scriptfm','Create')"><el-button type="primary" icon="Plus" @click="addAdmin">新增</el-button></el-form-item>
            </el-form>
        </div>
        <div class="table">
            <el-table  :height="'calc('+(tableHeight)+'px)'" border :data="tableData" ref="tableref" v-loading="loadingPage" style="width: 100%">
                <el-table-column type="index" width="60" align="center" label="序号">
                    <template #default="scope">
                        <span v-text="getIndex(scope.$index)"></span>
                    </template>
                </el-table-column>
                <el-table-column  prop="name" label="脚本名称" ></el-table-column>
                <el-table-column  prop="language_type" label="脚本语言" :show-overflow-tooltip='true'></el-table-column>
                <el-table-column  prop="exec_method" label="调用方式" :show-overflow-tooltip='true'></el-table-column>
                <el-table-column  prop="exec_param" label="脚本参数" :show-overflow-tooltip='true'></el-table-column>
                <el-table-column  prop="pre_exec_log" label="执行结果" :show-overflow-tooltip='true'>
                    <template #default="scope">
                        <span class="table-operate-btn" @click="listConent(scope.row, 1)">详情</span>
                    </template>
                </el-table-column>
                <el-table-column  prop="content" label="脚本内容">
                    <template #default="scope">
                        <span class="table-operate-btn" @click="listConent(scope.row, 2)">详情</span>
                    </template>
                </el-table-column>
                <el-table-column width="180" prop="create_datetime" label="创建时间"></el-table-column>
                <el-table-column label="操作" fixed="right" width="180">
                    <template #header>
                        <div style="display: flex;justify-content: space-between;align-items: center;">
                            <div>操作</div>
                            <div @click="setFull">
                                <el-tooltip content="全屏" placement="bottom">
                                    <el-icon ><full-screen /></el-icon>
                                </el-tooltip>
                            </div>
                        </div>
                    </template>
                    <template #default="scope">
                        <span class="table-operate-btn" @click="handleEdit(scope.row,'edit')" v-show="hasPermission('scriptfm','Update')">编辑</span>
                        <span class="table-operate-btn" style="color: #67C23A" @click="handleEdit(scope.row,'exec')" v-show="hasPermission('scriptfm','Update')">执行</span>
                        <span class="table-operate-btn" style="color: #F56C6C" @click="handleEdit(scope.row,'delete')" v-show="hasPermission('scriptfm','Delete')">删除</span>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
        <add-module-script ref="addModuleScriptFlag" @refreshData="getData"></add-module-script>
        <ly-dialog v-model="dialogVisible" :title="loadingTitle" width="760px" height="760px" :before-close="handleClose" >
            <aceEditor  v-model:value="content" v-if="flag === 2"></aceEditor>
            <el-table :data="hisTable" ref="tableref" v-loading="loadingPage" style="width: 100%" v-if="flag===1">
                <el-table-column  prop="date_time" label="执行时间" ></el-table-column>
                <el-table-column  prop="output" label="执行输出" ></el-table-column>
            </el-table>
        </ly-dialog>
    </div>
</template>

<script>
    import Pagination from "@/components/Pagination";
    import {dateFormats,getTableHeight} from "@/utils/util";
    import {ScriptList,ScriptDelete,ScriptExec} from '@/api/api'
    import addModuleScript from "./components/addModuleScript";
    import LyDialog from "@/components/dialog/dialog";
    import aceEditor from '@/components/editor/aceEditor'
    export default {
        name: "scriptfm",
        components: {addModuleScript, Pagination, LyDialog, aceEditor},
        data(){
            return{
                isFull:false,
                tableHeight:500,
                loadingPage:false,
                formInline:{
                    page: 1,
                    limit: 10,
                },
                pageparm: {
                    page: 1,
                    limit: 10,
                    total: 0
                },
                timers:[],
                tableData:[],
                dialogVisible: false,
                loadingTitle:'',
                content:'',
                hisTable: [],
                flag: ''
            }
        },
        methods:{
            // 表格序列号
            getIndex($index) {
                // (当前页 - 1) * 当前显示数据条数 + 当前行数据的索引 + 1
                return (this.pageparm.page-1)*this.pageparm.limit + $index +1
            },
            setFull(){
                this.isFull=!this.isFull
                window.dispatchEvent(new Event('resize'))
            },
            handleClose() {
                this.hisTable = [],
                this.content = '',
                this.dialogVisible = false
                this.loadingTitle = ''
            },
            listConent(obj, flag){
                this.dialogVisible = true
                this.flag = flag
                if (flag === 2){
                    this.loadingTitle = "脚本内容"
                    this.content = obj.content
                } else if (flag === 1){
                    
        
                    this.loadingTitle = "执行历史"
                    ScriptList({search: obj.id}).then(res => {
                        if(res.code ==2000) {
                            let _tmp = []
                            for (var i of res.data.data[0].pre_exec_log) {
                                _tmp.push(JSON.parse(i))
                            }
                            this.hisTable = _tmp
                        }
                    })

                }
            },
            changeStatus(row) {
                // console.log(row,'row----')
            },
            addAdmin() {
                this.$refs.addModuleScriptFlag.addUserFn(null,'新增')
            },
            handleEdit(row,flag) {
                if(flag=='edit') {
                    this.$refs.addModuleScriptFlag.addUserFn(row,'编辑')
                }
                else if(flag=='exec') {
                    ScriptExec(row).then(res=>{
                        if (res.code === 2000){
                            this.$message.success(res.msg)
                        } else {
                            this.$message.warning(res.msg)
                        }
                    })
                }
                else if(flag=='delete') {
                    let vm = this
                    vm.$confirm('您确定要删除选中的数据吗？',{
                        closeOnClickModal:false
                    }).then(res=>{
                        ScriptDelete({id:row.id}).then(res=>{
                            if(res.code == 2000) {
                                vm.$message.success(res.msg)
                                this.search()
                            } else {
                                vm.$message.warning(res.msg)
                            }
                        })
                    }).catch(()=>{

                    })
                }
                else if(flag=="reset"){
                    this.formInline = {
                        page:1,
                        limit: 10
                    }
                    this.pageparm={
                        page: 1,
                        limit: 10,
                        total: 0
                    }
                    this.getData()
                }
            },
            /**
             * 从URL里下载文件
            */
            // 下载文件
            downloadFileURL(url) {
                var iframe =document.createElement("iframe")
                iframe.style.display ="none";
                iframe.src = url;
                document.body.appendChild(iframe);
            },
            exportDataBackend() {
                // var params = {
                //     page: 1,
                //     limit: 9999,
                // }
                // UsersUsersExportexecl(params).then(res => {
                //      if(res.code ==2000) {
                //          this.downloadFileURL(res.data.data)
                //          //this.$message.warning(res.data.data)
                //      }
                //  })
            },
            callFather(parm) {
                this.formInline.page = parm.page
                this.formInline.limit = parm.limit
                this.getData()
            },
            search() {
                this.formInline.page = 1
                this.formInline.limit = 10
                this.getData()
            },
            //获取列表
            async getData() {
                this.loadingPage = true
                ScriptList(this.formInline).then(res => {
                     this.loadingPage = false
                     if(res.code ==2000) {
                         this.tableData = res.data.data
                         // console.log(res.data.data)
                         this.pageparm.page = res.data.page;
                         this.pageparm.limit = res.data.limit;
                         this.pageparm.total = res.data.total;
                     }
                 })
            },

            timeChange(val){
                if (val) {
                    this.formInline.beginAt=dateFormats(val[0],'yyyy-MM-dd hh:mm:ss');
                    this.formInline.endAt=dateFormats(val[1],'yyyy-MM-dd hh:mm:ss');
                } else {
                    this.formInline.beginAt = null
                    this.formInline.endAt = null
                }
                this.search()
            },
            // 计算搜索栏的高度
            listenResize() {
              this.$nextTick(() => {
                  this.getTheTableHeight()
              })
            },
            getTheTableHeight(){
                let tabSelectHeight = this.$refs.tableSelect?this.$refs.tableSelect.offsetHeight:0
                tabSelectHeight = this.isFull?tabSelectHeight - 110:tabSelectHeight
                this.tableHeight =  getTableHeight(tabSelectHeight)
            }

        },
        created() {
            this.getData()
        },
        mounted() {
            // 监听页面宽度变化搜索框的高度
            window.addEventListener('resize', this.listenResize);
            this.$nextTick(() => {
                this.getTheTableHeight()
            })
        },

        unmounted() {
             // 页面销毁，去掉监听事件
			window.removeEventListener("resize", this.listenResize);
        },

    }
</script>

<style lang="scss" scoped>
    .lycontainer{
        background: var(--el-bg-color);
        padding: 10px;
        height: calc(100vh - 220px) ;
        /*background: black;*/
        /*padding: 10px;*/
    }
    ::v-deep(.el-tabs__header){
        margin: 0 0 1px;
    }
</style>